package com.guigu.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.OrderDetailsMapper;
import com.guigu.mapper.OrdersMapper;
import com.guigu.mapper.ProductMapper;
import com.guigu.pojo.OrderDetails;
import com.guigu.service.OrderDetailsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

import java.util.List;

/**
 * <p>
 * 订单明细表 服务实现类
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@Service
public class OrderDetailsServiceImpl extends ServiceImpl<OrderDetailsMapper, OrderDetails> implements OrderDetailsService {

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Override
    public Page<OrderDetails> getAllOrderDetailsByOrderNumber(HashMap<String, Object> map) {
        Integer currentPage = 1;
        Integer pageSize = 5;

        Object pageNumObj = map.get("currentPage");
        if (pageNumObj != null) {
            if (pageNumObj instanceof String && StringUtils.isNotBlank((String) pageNumObj)) {
                currentPage = Integer.parseInt((String) pageNumObj);
            } else if (pageNumObj instanceof Integer) {
                currentPage = (Integer) pageNumObj;
            }
        }
        Object pageSizeObj = map.get("pageSize");
        if (pageSizeObj != null) {
            if (pageSizeObj instanceof String && StringUtils.isNotBlank((String) pageSizeObj)) {
                pageSize = Integer.parseInt((String) pageSizeObj);
            } else if (pageSizeObj instanceof Integer) {
                pageSize = (Integer) pageSizeObj;
            }
        }

        Page<OrderDetails> page = new Page<>(currentPage, pageSize);
        // 确保map不为null
        if (map == null) {
            map = new HashMap<>();
        }

        List<OrderDetails> orderDetails = orderDetailsMapper.getOrdersDetails(map);
        System.out.println(orderDetails);
        if (orderDetails != null) {
            page.setRecords(orderDetails);
            return page;
        }
        return null;
    }

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private ProductMapper productMapper;


    @Override
    public List<OrderDetails> getOrderDetailsByOrderId(Integer orderId) {
        QueryWrapper<OrderDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orders_id",orderId);
        if (orderDetailsMapper.selectList(queryWrapper)!=null){
            List<OrderDetails> orderDetails = orderDetailsMapper.selectList(queryWrapper);
            for (OrderDetails orderDetail : orderDetails) {
                orderDetail.setProduct(productMapper.selectById(orderDetail.getProductId()));
            }
            return orderDetails;
        }
        return null;
    }
}
